﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Config;
using NLog.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;

namespace TPMS_ScanComparison
{
    internal static class Program
    {

        private static Mutex _mutex;
        /// 
        [STAThread]
        static void Main()
        {
            // 确保程序只运行一个实例
            const string mutexName = "YourCompanyName.YourApplicationName.UniqueMutexIdentifier";
            bool createdNew;
            _mutex = new Mutex(true, mutexName, out createdNew);

            if (!createdNew)
            {
                MessageBox.Show("程序已经在运行中", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            try
            {
                //拿到实体对象
                var logger = LogManager.GetCurrentClassLogger();
                //配置log
                var config = new ConfigurationBuilder()
                    .SetBasePath(Environment.CurrentDirectory)
                    .AddJsonFile("appsettings.json")
                    .Build();
                //读取配置
                var nlogConfig = config.GetSection("NLog");
                //关联配置
                LogManager.Configuration = new NLogLoggingConfiguration(nlogConfig);

                ////创建服务集合依赖注入
                //var services = new ServiceCollection()
                //    .AddLogging(logbuilder => {
                //        logbuilder.ClearProviders();
                //        logbuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
                //        logbuilder.AddNLog();

                //    });
                ////添加对象到服务
                //services.AddScoped<person>();
                ////创建服务定位器
                //var serviceProvider = services.BuildServiceProvider();
                //Global.ServiceProvider = serviceProvider;
                ////取出对象
                //var person = serviceProvider.GetRequiredService<person>();


                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FormMain());
            }
            finally
            {
                // 释放Mutex
                _mutex?.ReleaseMutex();
            }
        }


    }

 
}
